//2079.给植物浇水
//https://leetcode.cn/problems/watering-plants/?envType=daily-question&envId=2024-05-08
class Solution {
public:
    int wateringPlants(vector<int>& plants, int capacity) {
        int water = 0; //取水点
        int n = plants.size();
        int cap = capacity;
        int ret = 0;
        for (int i = 0; i < n; ++i)
        {
            if (plants[i] == -1)
            {
                water = i;
                continue;
            }
            if(cap >= plants[i]) ++ret;
            while (cap < plants[i])
            {
                ret += (i - water) * 2 + 1;
                cap = capacity;
            }
            cap -= plants[i];
        }
        return ret;
    }
};